const http = require('node:http')
const fs = require('node:fs')
const path = require('node:path')
const mime = require('mime-types')
const { log } = require('node:console')
http.createServer((req, res) => {
    const { method, url } = req
    // 静态资源获取   get请求 并且 请求目录为 static
    if (method === 'GET' && url.startsWith('/static')) {
        const staticPath = path.resolve(__dirname, './' + url)
        console.log('staticPath', __dirname, './' + staticPath)
        fs.readFile(staticPath, (err, data) => {
            if (err) {
                res.writeHead(404, {
                    'content-type': 'text/aplin'
                })
                res.end('not found')
            } else {
                const mimeType = mime.lookup(staticPath)
                console.log('测试是否缓存')
                res.writeHead(200, {
                    'content-type': mimeType,  //mime 类型
                    "cache-control": "public, max-age=3600"
                })
                res.end(data)
            }
        })
    }
    if ((method === 'GET' || method === 'POST') && url.startsWith('/api')) {
        // 这里认为是动态资源
        // 可以写接口逻辑
    }

}).listen(9999, () => {
    console.log('9999端口启动了')
})